//
//  MTValidationCodeController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/5.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTValidationCodeController.h"
#import "MTCompationInfoController.h"
#import "BONSTimerQueue.h"
#import "MTSetWalletPasswordController.h"
@interface MTValidationCodeController ()
@property (weak, nonatomic) IBOutlet UIButton *sureButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UITextField *vercodeTextField;

@property (weak, nonatomic) IBOutlet UILabel *tipLable;
@property (weak, nonatomic) IBOutlet UILabel *phoneLable;
@property (weak, nonatomic) IBOutlet UIButton *repeatButton;

@property(nonatomic,assign) int count;

@end

@implementation MTValidationCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
      _count = 60;
    self.titleLable.text = NSLTLocalizedString(@"register.inputverCode", nil);
    self.titleLable.text = NSLTLocalizedString(@"register.getvercoidetip", nil);
    self.vercodeTextField.placeholder = NSLTLocalizedString(@"register.inputverCode", nil);
    [self.repeatButton setTitle:NSLTLocalizedString(@"register.re_send", nil) forState:UIControlStateNormal];
    if([self.contryCode containsString:@"+"])
    {
        self.phoneLable.text = [NSString stringWithFormat:@"%@ %@",self.contryCode,self.phone];
    }else
    {
        self.phoneLable.text = [NSString stringWithFormat:@"+%@ %@",self.contryCode,self.phone];
    }
    
    
    [self.sureButton setTitle:NSLTLocalizedString(@"main.sure", nil) forState:UIControlStateNormal];
    RACSignal* textSigal =  [[self.vercodeTextField rac_textSignal] map:^id _Nullable(NSString * _Nullable text) {
        if(text.length > 3)
        {
            return @(YES);
        }else
        {
            return  @(NO);
        }
    }];
    [textSigal subscribeNext:^(id  _Nullable x) {
        BOOL enable = [x boolValue];
        if(enable)
        {
            self.sureButton.backgroundColor = mainButtonColor ;
            [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureButton.enabled = YES;
        }else
        {
            self.sureButton.backgroundColor = UIColorFromRGB(0xdfdfdf);
            [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureButton.enabled = NO;
        }
    }];
    [self.vercodeTextField becomeFirstResponder];
    [[BONSTimerQueue shareTimer] addEvent:@selector(timeClick) target:self timerWithTimeInterval:1];
    
    if(_needRequstVerCode)
    {
        [self reteryGetCodeClick:nil];
        _count = 0;
        self.repeatButton.enabled = YES;
        [self.repeatButton setTitle:NSLTLocalizedString(@"register.re_send", nil) forState:UIControlStateNormal];
        [self.repeatButton setTitleColor:UIColorFromRGB(0x34a9ff) forState:UIControlStateNormal];
        [[BONSTimerQueue shareTimer] removeEcvent:@selector(timeClick) target:self];
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

-(void)timeClick
{
    if(--_count)
    {
        [self.repeatButton setTitle:[NSString stringWithFormat:@"%@%@",NSLTLocalizedString(@"register.re_send", nil),@(_count)] forState:UIControlStateNormal];
        [self.repeatButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        self.repeatButton.enabled = NO;
    }else
    {
         self.repeatButton.enabled = YES;
        [self.repeatButton setTitle:NSLTLocalizedString(@"register.re_send", nil) forState:UIControlStateNormal];
        [self.repeatButton setTitleColor:UIColorFromRGB(0x34a9ff) forState:UIControlStateNormal];
        [[BONSTimerQueue shareTimer] removeEcvent:@selector(timeClick) target:self];
    }
    
}

- (IBAction)reteryGetCodeClick:(UIButton *)sender {
    
    
    [MBProgressHUD showProcessHud:@"" toView:nil];
    void(^complationBlock)(MTError *error) = ^(MTError *error)
    {
        [MBProgressHUD hideHUD];
        if(error == nil)
        {
            self.count = 60;
            [self.repeatButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [self.repeatButton setTitle:[NSString stringWithFormat:@"%@%@",NSLTLocalizedString(@"register.re_send", nil),@(self.count)] forState:UIControlStateNormal];
            [[BONSTimerQueue shareTimer] addEvent:@selector(timeClick) target:self timerWithTimeInterval:1];
        }else
        {
            [MBProgressHUD showError:error.errorDescription];
        }
    };
    if(_validationCodeType == MTValidationCodeRegister)
    {
      [[MTUserApiManager manager] getregisterVerCode:self.phone countryCode:self.contryCode completion:complationBlock];
    }else if(_validationCodeType == MTValidationCodeSetWalletPassword)
    {
        NSDictionary *param = @{@"countryCode":self.contryCode,@"phone":self.phone};
        [[MTHttpTool shareTool] getPath:@"/wallet/get_set_pas_verify_code" parameters:param success:^(MTBaseData *datamodel) {
            complationBlock(nil);
        } failer:complationBlock];
    }
}



- (IBAction)sureClick:(UIButton *)sender {
    
    [MBProgressHUD showProcessHud:@"" toView:self.view];
    void(^complationBlock)(MTError *error) = ^(MTError *mterror)
    {
        [MBProgressHUD hideHUDForView:self.view];
        if(mterror == nil)
        {
            if(_validationCodeType == MTValidationCodeRegister)
            {
  
            MTCompationInfoController *compationVc = [MTCompationInfoController baseStoryBordWithName:@"Login"];
            compationVc.phone = self.phone;
            compationVc.contryCode = self.contryCode;
            compationVc.verCode = self.vercodeTextField.text;
            [self.navigationController pushViewController:compationVc animated:NO];
                
            }else if(_validationCodeType == MTValidationCodeSetWalletPassword){

            MTSetWalletPasswordController *setpassVc = [[MTSetWalletPasswordController alloc] init];
            setpassVc.phone = self.phone;
            setpassVc.countryCode = self.contryCode;
            setpassVc.verCode = self.vercodeTextField.text;
            [self.navigationController pushViewController:setpassVc animated:NO];
            }
            
        }else
        {
            [MBProgressHUD showError:mterror.errorDescription];
        }
    };
    
    if(_validationCodeType == MTValidationCodeRegister)
    {
        [[MTVerCodeApiManager manager] verifyCodeWithPhone:self.phone countryCode:self.contryCode code:self.vercodeTextField.text completion:complationBlock];
    }else if(_validationCodeType == MTValidationCodeSetWalletPassword)
    {
        NSDictionary *param = @{@"countryCode":self.contryCode,
                                @"phone":self.phone,
                                @"code":self.vercodeTextField.text};
        [[MTHttpTool shareTool] getPath:@"/wallet/verify_code" parameters:param success:^(MTBaseData *datamodel) {
            complationBlock(nil);
        } failer:complationBlock];
    }
}

@end
